﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.VoteManage
{
	public partial class ModifyVote : BasePage
	{
		private int _identifier;
		private bool _isSetIdentifier = false;

		/// <summary>
		/// 当前修改信息的标识符。
		/// </summary>
		public int identifier
		{
			get
			{
				if (!_isSetIdentifier)
				{
					if (!String.IsNullOrEmpty(Request.QueryString["VoteID"]))
						_identifier = Convert.ToInt32(Request.QueryString["VoteID"]);
					_isSetIdentifier = true;
				}

				return _identifier;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
				{
					InitData();
					this.DataBind();
				}
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				Vote info = UCVote.Info;
				info.VoteId = identifier;

				Votes.Update(info);
				UpdateComplete();
			}
		}

		/// <summary>
		/// 初始化数据。
		/// </summary>
		public void InitData()
		{
			Vote info = Votes.GetItem(identifier, true);
			UCVote.Info = info;
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Votes.GetUpdateRight(Votes.GetItem(identifier));
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 更新操作完成，显示消息提示。
		/// </summary>
		public void UpdateComplete()
		{
			Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Update));
		}
	}
}
